package edu.csumb.itcd.teammario;
import java.awt.Point;
import java.awt.Rectangle;
import java.awt.geom.Ellipse2D;
import java.util.ArrayList;
import java.util.Random;


public class Collision implements Runnable {


    Thread collisionThread;
    private Point playerPosition = new Point(0,0);

    
    public Collision()
    {
        collisionThread = new Thread(this);
        collisionThread.start();
    }


    
    @Override
    public void run() {
        // TODO Auto-generated method stub
    	while (true)
    	{
    		if (Setup.isPaused() == false)
    		{
    		synchronized (EnemyList.enemyArray)
            {
	    		synchronized (BulletList.getPlayerBulletArray())
	            {
	                for (Bullet bullet : BulletList.getPlayerBulletArray()){
	                    
	                    // check for collision of player bullets with enemies
	
                        for( Enemy enemy : EnemyList.enemyArray)
                        {
                            if (bullet.getBulletImage().intersects(enemy.getEnemyImage()))
                            {
                                enemy.setCurrentHealth(enemy.getCurrentHealth() - bullet.getDamage());
                                bullet.enemyOnHit(enemy);
                                bullet.setIsActive(false);
                                bullet.setDamage(0);
                                Player.incrementShotsLanded();
//                                System.out.println("Bullet Hit");
                            }
                                
                        }    
                    }
	                        
                }
            }
                    // check for collision of enemy bullets with player

            synchronized (BulletList.getEnemyBulletArray())
            {
                for (Bullet bullet : BulletList.getEnemyBulletArray())
                {    
                    if (bullet.getBulletImage().intersects(Player.getPlayerImage()))
                    {
                        Player.setCurrentHealth(Player.getCurrentHealth() - bullet.getDamage());
                        bullet.playerOnHit();
                        bullet.setIsActive(false);
                        bullet.setDamage(0);
                        
//                                System.out.println("Bullet Hit");
                    }
                                
                }
                    
            }
            // Check Collision with enemy touching player
            synchronized (EnemyList.enemyArray)
            {
                for( Enemy enemy : EnemyList.enemyArray){
                    if (enemy.getEnemyImage().intersects(Player.getPlayerImage()))
                    {
                        Player.setCurrentHealth(Player.getCurrentHealth() - enemy.getDamage());
//                        System.out.println("Player Hit");
                    }
                }
            }
            
            synchronized (EnemyList.enemyArray)
            { 
	            synchronized (BulletList.getTowerBulletArray())
	            {
	                for (Bullet bullet : BulletList.getTowerBulletArray()){
	                    
	                    // check for collision of tower bullets with enemies
	
	                    for( Enemy enemy : EnemyList.enemyArray)
                        {
                            if (bullet.getBulletImage().intersects(enemy.getEnemyImage()))
                            {
                                enemy.setCurrentHealth(enemy.getCurrentHealth() - bullet.getDamage());
                                bullet.enemyOnHit(enemy);
                                bullet.setIsActive(false);
                                bullet.setDamage(0);
//                                System.out.println("Bullet Hit");
                            }
                            
                        }                     
	                }
	            }
    	}

            synchronized (EnemyList.getMoneyArray())
            {
            	for (Money money : EnemyList.getMoneyArray())
            	{
            		if (money.getMoneyImage().intersects(Player.getPlayerImage()))
            		{
            			money.setCollected(true);
            			Player.setMoney(Player.getMoney() + money.getValue());
            		}
            	}
            }
    		}
            try
            {
                Thread.sleep(30);
            }
            catch(InterruptedException ex)
            {
                
            }
    	}
            
}
    public void updatePlayerPosition(Point playerPosition)
    {
        this.playerPosition = playerPosition;
    }
}

